package servlet.zhouchao;

import pojo.Goods;
import service.GoodsClassService;
import service.GoodsService;
import service.impl.GoodsClassServiceImpl;
import service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

@WebServlet(value = "/AddGoodsSubmitServlet")
public class AddGoodsSubmitServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String goname=request.getParameter("goname");
        String volume=request.getParameter("volume");
        String praise=request.getParameter("praise");
        String uptime=request.getParameter("uptime");
        String url=request.getParameter("url");
        String describe=request.getParameter("describe");
        String cost=request.getParameter("cost");
        String price=request.getParameter("price");
        String stock=request.getParameter("stock");
        String state=request.getParameter("state");
        String clId=request.getParameter("goodsClass");
        String goSecKill=request.getParameter("goSecKill");

        Goods goods=new Goods();
        GoodsClassService goodsClassService=new GoodsClassServiceImpl();
        goods.setGoName(goname);
        goods.setGoSalesVolume(Integer.parseInt(volume));
        goods.setGoPraise(Integer.parseInt(praise));
        goods.setGoUptime(Timestamp.valueOf(uptime));
        goods.setGoPictureUrl(url);
        goods.setGoDescribe(describe);
        goods.setGoCost(Integer.parseInt(cost));
        goods.setGoPrice(Integer.parseInt(price));
        goods.setGoStock(Integer.parseInt(stock));
        goods.setGoState(Integer.parseInt(state));
        goods.setClId(Integer.parseInt(clId));
        goods.setGoSecKill(Integer.parseInt(goSecKill));

        GoodsService goodsService=new GoodsServiceImpl();
        int i=goodsService.addGoods(goods);
        if(i>0){
            request.getRequestDispatcher("GoodsPageBeanListServlet").forward(request,response);
        }
        else{
            response.getWriter().print("新增失败");
        }

    }
}
